<?php

namespace App\Modules\Index\Models;


use Illuminate\Database\Eloquent\Model;

/**
 * @package App\Modules\Index\Models
 */
class  Prescription extends Model
{
    protected $table = 'prescription';

    protected $fillable = ['id','user_id', 'meals', 'type','group_id','doctor_id', 'insulin_name_en','insulin_name_cn','recommendation_type','insulin_type','adjustment_units','initial_units','final_units','insulin_preparation','audit_at','created_at','updated_at','status','is_read'];

    //后台使用
    static function getList($param)
    {
        return self::where('recipient_id','=',0)->orWhere('recipient_id','=',$param['user_id'])->orderBy('created_at','desc')->paginate();
    }


    static function GetPrescriptions($id)
    {
        return self::where('id',$id)->first();
    }


    static function addPrescription($param)
    {
        // TODO check
        return self::create($param);
    }


    static function updatePrescription($param)
    {
        // TODO check
        return self::where('id',$param['id'])->update($param);
    }

    static function getPrescriptionOne($param){
        $result = self::where('insulin_name_en',$param['insulin_name_en'])->where('insulin_name_cn',$param['insulin_name_cn'])->where('insulin_type',$param['insulin_type'])->where('insulin_preparation',$param['insulin_preparation']);
        if ( !empty($param['id']) ) {
            $result = $result->where('id',$param['id']);
        }
        if ( !empty($param['group_id']) ) {
            $result = $result->where('group_id',$param['group_id']);
        }
        if ( !empty($param['meals']) ) {
            $result = $result->where('meals',$param['meals']);
        }
        return $result->count();
    }

    static function updateGroupPrescription($param) {
        return self::where('group_id',$param['group_id'])->update($param);
    }
}